﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using OpenWings.Entities;

namespace OpenWings.Storage
{
    public class XmlStorage<TEntity>
    {
        string _Path;

        public string Path
        {
            get { return _Path; }
            set { _Path = value; }
        }

        XmlSerializer _Serializer;
        
        public XmlStorage(string path)
        {
            _Serializer = new XmlSerializer(typeof(TEntity));
            _Path = System.IO.Path.GetFullPath(path);
        }

        public TEntity Load()
        {
            using (FileStream MyStream = new FileStream(_Path, FileMode.Open))
                return (TEntity)_Serializer.Deserialize(MyStream);
        }

        public void Save(TEntity entity)
        {
            using (FileStream MyStream = new FileStream(_Path, FileMode.Create))
            {
                _Serializer.Serialize(MyStream, entity);
                MyStream.Close();
            }
        }
    }
}
